package com.msmall.community.klg.preparer;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.ViewPreparer;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.Constants;
import com.msmall.community.ApplicationManager;
import com.msmall.community.common.CascadeCollection;
import com.msmall.community.dpt.model.Segment;
import com.msmall.community.klg.model.Article;
import com.msmall.community.klg.service.ArticleManager;

public class ArticleViewFooterPreparer implements ViewPreparer{
	private static final Log log = LogFactory.getLog(ArticleViewFooterPreparer.class);
	public void execute(TilesRequestContext tilesContext, AttributeContext context) {
		try{
			HttpServletRequest request = (HttpServletRequest) tilesContext.getRequest();
			Long segid = (Long) context.getAttribute("segid").getValue();
			Long articleid = (Long) context.getAttribute("articleid").getValue();
			
			ArticleManager articleManager = (ArticleManager) ApplicationManager.appContext.getBean("articleManager");
			
			List list = articleManager.getPreArticle(articleid, segid, true, 1);
			if(list != null && list.size() >0){
				request.setAttribute("articleViewFooterPre", list.get(0));
			}
			list =  articleManager.getNextArticle(articleid, segid, true, 1);
			if(list != null && list.size() >0){
				request.setAttribute("articleViewFooterNext", list.get(0));
			}
			
			Map<Long, List<Article>> klgSubSegmentHot = (Map<Long, List<Article>>)CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_KLG_SUBSEGMENT_HOT);
			List<Article> articles =null;
			if(klgSubSegmentHot != null){
				CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
				articles = klgSubSegmentHot.get(segid);
			}
			request.setAttribute("articleViewFooterRelations", articles);
		}catch(Exception e){
			log.debug(e);
		}
	}
}